package engine.ui.icon
{
	import engine.data.BindManage;
	import engine.data.ViewObject;
	
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import newx.com.ResManager;
	import newx.com.control.NxClip;

	/**
	 *布阵显示单元 
	 * @author yanghr
	 * 
	 */	
	public class PicViewIcon extends Sprite
	{
		private var _dataobj:ViewObject;
		
		/**
		 *数据类 
		 */
		public function get dataobj():ViewObject
		{
			return _dataobj;
		}
		private var blindflag:int=-1;
		/**
		 * @private
		 */
		public function set dataobj(value:ViewObject):void
		{
			if(_dataobj==value)
				return;
			_dataobj = value;
		}
		public function PicViewIcon()
		{
			super();
			init()
		}
		protected function init():void
		{
			bg=new NxClip;
			bg.x=bg.y=3
			addChild(bg);

			
		}
		public function clear():void
		{
			bg.clear();
		}
		/**
		 * 物品图片
		 */		
		protected var bg:NxClip;
		private var _id:uint;
		public function get id():uint
		{
			return _id
		}
		public function set id(value:uint):void
		{
			_id=value;
				bg.source=(RES_PATH||"res/items/icon_")+_id+(FORMAT||".jpg");
		}
		public var RES_PATH:String;
		public var FORMAT:String
		private var _index:int
		public function get index():int
		{
			return _index;
		}
		
		public function set index(value:int):void
		{
			_index = value;
		}
		/**
		 *拖拽完成消息 
		 */		
		public static const DRAG_COMPLETE:String="drag complete"
		private var dragBg:NxClip;
		public function addAllListener():void
		{
			addEventListener(MouseEvent.MOUSE_DOWN,starDrag);
		}
		private function starDrag(e:Event):void
		{
			if(dragBg==null)
			{
				dragBg=ResManager.copy(bg) as NxClip;
			}
			dragBg.mouseEnabled=dragBg.mouseChildren=false;
			if(dragBg.source!=bg.source)
				dragBg.source=bg.source
			if(!dragBg.parent&&stage)
			{
				stage.addChildAt(dragBg,stage.numChildren-1);
				var point:Point=new Point(bg.x,bg.y);
				var np:Point=localToGlobal(point)
				dragBg.x=np.x;
				dragBg.y=np.y;
				
			}
			//			dragBg.source=bg.source
			removeEventListener(MouseEvent.MOUSE_DOWN,starDrag);
			stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
			dragBg.startDrag();
		}
		private function endDrag(e:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
			addEventListener(MouseEvent.MOUSE_DOWN,starDrag);
			dragBg.stopDrag();
			if(dragBg.parent)
				dragBg.parent.removeChild(dragBg);
			var ee:MouseEvent=new MouseEvent(DRAG_COMPLETE,true,false,e.localX,e.localY,e.target as InteractiveObject)
			dispatchEvent(ee);
		}
		public function removeAllListener():void
		{
			removeEventListener(MouseEvent.MOUSE_DOWN,starDrag);
			if(stage)
				stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
		}
		
		public function set enableDrag(value:Boolean):void
		{
			if(value)
			{
				addEventListener(MouseEvent.MOUSE_DOWN,starDrag);
			}
			else
			{
				removeEventListener(MouseEvent.MOUSE_DOWN,starDrag);
			}
		}
		/**
		 *（value>0）？是上锁:没上锁 
		 * @param value
		 * @see LockIcon.LOCK
		 * 
		 */		
		public function set state(value:int):void
		{
			// TODO Auto Generated method stub
		}
		
		public function get state():int
		{
			// TODO Auto Generated method stub
			return 0
		}
		
		public function setSource(value:*, state:int=0):void
		{
			
		}
	}
}